package at.playbox.server.businesslogic;

import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import at.playbox.server.entity.Response;
import at.playbox.server.entity.Song;
import at.playbox.server.entity.SongList;
import at.playbox.server.entity.SongUser;

public class ShopService
{

	// HashMap um Objekte vom Typ SongUser im RAM zu halten
	public static Map<String, SongUser> songUserMap = new HashMap<String, SongUser>();

	// Methode gibt alle verfügbaren Songs als XML zurück
	public SongList getAllSongs() throws Exception
	{
		List<Song> songs = new ArrayList<Song>();

		for (int i = 1; i < 4; i++)
		{
			Song song = new Song();
			song = getSongById(i);
			songs.add((Song) song);
		}
		SongList songList = new SongList();
		songList.setSongs(songs);
		return songList;
	}

	// Methode um Song zu kaufen, dabei wird überprüft ob es den Song überhaupt
	// gibt und ob der User diesen Song bereits gekauft hat.
	public Response buySong(int id, String username)
	{
		Response response = new Response();
		if (id < 4)
		{
			if (checkSongUserMap(id, username))
			{
				response.setResponse("[Fault] Song already bought!");
			}
			else
			{
				SongUser songUser = new SongUser();
				songUser.setSongId(id);
				songUser.setUsername(username);
				songUserMap.put(username + ";" + id, songUser);
				response.setResponse("OK");
			}
		}
		else
		{
			response.setResponse("[Fault] No song with id=" + id + " exists!");
		}
		return response;
	}

	// Methode um zu überprüfen, ob ein bestimmter User einen bestimmten Song
	// gekauft hat
	public boolean checkSongUserMap(Integer id, String username)
	{
		System.out.println(username + ";" + id);
		if (songUserMap.containsKey(username + ";" + id))
		{
			System.out.println("checkSongUserMap true");
			return true;
		}
		else
		{
			System.out.println("checkSongUserMap false");
			return false;
		}
	}

	// Methode gibt FileStream zurück, um Song downloaden zu können.
	public File downloadSong(int id, String username)
	{
		if (id < 4)
		{
			if (checkSongUserMap(id, username))
			{
				return new File(this.getClass().getResource("/at/playbox/server/files/" + id + ".wav").getFile());
			}
			else
			{
				System.out.println("Nullllllll");
				return null;
			}
		}
		else
		{
			return null;
		}
	}

	// "DB" der Songs
	protected Song getSongById(int id) throws Exception
	{
		switch (id)
		{
		case 1:
			Song song1 = new Song();
			song1.setId(1);
			song1.setArtist("Peitro Lombardi");
			song1.setTitle("Call My Name");
			song1.setPrice(5.4);
			return song1;
		case 2:
			Song song2 = new Song();
			song2.setId(2);
			song2.setArtist("LMFAO feat. Lauren...");
			song2.setTitle("Party Rock Anthem");
			song2.setPrice(4.4);
			return song2;
		case 3:
			Song song3 = new Song();
			song3.setId(3);
			song3.setArtist("Bruno Mars");
			song3.setTitle("The Lazy Song");
			song3.setPrice(5.4);
			return song3;
		default:
			throw new Exception("wrong ID!");
		}
	}

	public String createXML(Object obj) throws JAXBException
	{
		StringWriter sw = new StringWriter();
		JAXBContext context = JAXBContext.newInstance(obj.getClass());
		Marshaller marshaller = context.createMarshaller();
		marshaller.marshal(obj, sw);
		return sw.toString();
	}

}
